<template>
	<view style="padding: 40rpx;">
		<view style="font-size: 50rpx;">注册账号</view>
		<view style="font-size: 30rpx;color: #9b9696;margin-bottom: 50rpx;">欢迎使用智慧环保app</view>
		<view class="content_realization">
			手机号码
			<u-input border="none" class="realization_input" placeholder="请输入手机号" v-model="user.phone">
				<u--text text="+86 |" slot="prefix" margin="0 3px 0 0" type="tips"></u--text>
			</u-input>
			验证码
			<u-input border="none" class="realization_input" placeholder="请输入验证码" v-model="codeNum">
				<template slot="suffix">
					<u-code ref="uCode" @change="codeChange" seconds="20" changeText="X秒后重新获取"></u-code>
					<u-button @tap="getCode" :text="tips" style="color: #00c297;" type="info" size="mini"></u-button>
				</template>
			</u-input>
			密码
			<u-input :password="true" border="none" class="realization_input" placeholder="请输入密码" v-model="user.password"></u-input>
			<view style="display: flex;justify-content: flex-start;">
				<u-checkbox-group placement="row">
					<u-checkbox @change="checkbox" activeColor="#00c297" labelSize="10" label="我已阅读并同意">
					</u-checkbox>
				</u-checkbox-group>
				<text style="font-size: 10px;color: #00c297;">《用户隐私政策》</text>
			</view>
		</view>
		<view style="margin-bottom: 20rpx;">
			<button @click="registerGoTo" style="background-color: #00c297;color: #ffffff;">立即注册</button>
		</view>
		<view style="width: 100%;text-align: center;color: #00c297;"><text @click="toLogin">已有账号，去登录</text></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '获取验证码',
				user:{},
				codeNum: '',
				checkboxChecked: false
			}
		},
		methods: {
			toLogin(){
				uni.navigateTo({
					url:"../login/login"
				})
			},
			registerGoTo(){
				if (this.user.phone.length == 0) {
					uni.showToast({
						title: '请输入账号！',
						icon: 'none'
					})
				} else if (this.codeNum.length == 0) {
					uni.showToast({
						title: '请输入验证码！',
						icon: 'none'
					})
				} else if (this.user.password.length == 0) {
					uni.showToast({
						title: '请输入密码！',
						icon: 'none'
					})
				} else if (!this.checkboxChecked) {
					uni.showToast({
						title: '请同意用户隐私政策',
						icon: 'none'
					})
				} else{
					this.user.id="普通用户id:"+this.user.phone
					this.user.nickname="智慧环保:"+this.user.phone
					this.user.avatarUrl="https://img1.baidu.com/it/u=1966616150,2146512490&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1657558800&t=8cdd72cba9517e46a547609cb95dc841"
					// 调用验证码验证接口
					
					// 调用注册接口
					this.$api.iUserRegister(this.user).then(res=>{
						console.log(res);
						if(res.code==200){
							uni.$u.toast(res.msg)
							uni.navigateTo({
								url:"../login/userLogin"
							})
						}else{
							uni.$u.toast(res.msg)
						}
					})
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			checkbox() {
				this.checkboxChecked = !this.checkboxChecked
				console.log(this.checkboxChecked);
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			}
		}
	}
</script>

<style>
	.realization_input {
		background-color: #f2f7fb;
		height: 80rpx;
	}

	.content_realization {
		line-height: 80rpx;
	}
</style>
